<?php

namespace app\common\library;
use app\common\model\UserPasswordList;
class YuPasswordAdmin
{
    const VERSION = '6.0.5';

    protected $errorString = '';

    public static $instance ;

    /**
     * @title 单例调用
     * @description 单例调用
     * @author Admin ldy
     * @time 2018/9/25  12:22
     */
    public static function getInstance(){
        if (YuPasswordAdmin::$instance == null){
            YuPasswordAdmin::$instance = new \app\common\library\YuPasswordAdmin;
            return YuPasswordAdmin::$instance;
        }else{
            return YuPasswordAdmin::$instance;
        }
    }

    public function __construct(){
        //初始化
    }

    public function setErrorString($msg){
        $this->errorString = $msg;
    }

    public function getErrorString(){
        return $this->errorString;
    }

    public static function addPassword($uid,$option){
        if (empty($option)||empty($uid)){
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        $userPass = new UserPasswordList();
        $userPass['title'] = $option['title'];
        $userPass['account'] = $option['account'];
        $userPass['password'] = encode($option['password']);
        $userPass['user_id'] = $uid;
        $userPass['login_url'] = $option['login_url'];
        $userPass['sort'] = $option['sort'];
        $userPass['desc'] = $option['desc'];
        $re = $userPass->save();
        if ($re){
            return new ReSuccess('插入成功！',$userPass);
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }

    public static function changePassword($id,$option){
        if (empty($option)||empty($id)){
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        $where=['id'=>$id];
        $userPass = UserPasswordList::where($where)->find();
        $userPass['title'] = $option['title'];
        $userPass['account'] = $option['account'];
        $userPass['password'] = encode($option['password']);
        $userPass['login_url'] = $option['login_url'];
        $userPass['sort'] = $option['sort'];
        $userPass['desc']=$option['desc'];
        $re = $userPass->save();
        if ($re){
            return new ReSuccess('修改成功！',$userPass);
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }

    public static function delPassword($id){
        if (empty($id)){
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        $where=['id'=>$id];
        $userPass = UserPasswordList::where($where)->find();
        $userPass['status'] = 1;
        $re = $userPass->save();
        if ($re){
            return new ReSuccess('删除成功！',$userPass);
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }

    public static function findPasswordById($id){
        if (empty($id)){
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        $where=['id'=>$id];
        $userPass = UserPasswordList::where($where)->find();
        $userPass['password'] = func_substr_replace(decode($userPass['password']));
        if ($userPass){
            return new ReSuccess('获取数据成功！',$userPass);
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }

    public static function findPasswordByUid($uid){
        if (empty($uid)){
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        $where=['user_id'=>$uid];
        $userPass = UserPasswordList::where($where)->order('sort')->select();
        if ($userPass){
            foreach ($userPass as $key=>$item){
                $str = decode($item['password']);
                $strlen = strlen($str);
                $item['password'] = func_substr_replace($str,'*',2,$strlen-4);
            }
            return new ReSuccess('获取数据成功！',$userPass);
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }

    public static function getRealPassWord($id){
        if (empty($id)){
            return new ReFail(ErrorType::DATA_EXCEPTION);
        }
        $where=['id'=>$id];
        $userPass = UserPasswordList::where($where)->find();
        $userPass['password'] = decode($userPass['password']);
        if ($userPass){
            return new ReSuccess('获取数据成功！',$userPass);
        }else{
            return new ReFail(ErrorType::ACTION_FAILED);
        }
    }


}
